package cn.iocoder.yudao.module.biz.controller.admin.expand.targets.channel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.channel.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.channel.ExpandTargetChannelDO;
import cn.iocoder.yudao.module.biz.service.expand.targets.channel.ExpandTargetChannelService;

@Tag(name = "管理后台 - 拓展目标_渠道")
@RestController
@RequestMapping("/san/biz-expand-target-channel")
@Validated
public class ExpandTargetChannelController {

    @Resource
    private ExpandTargetChannelService expandTargetChannelService;

    @PostMapping("/create")
    @Operation(summary = "创建拓展目标_渠道")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:create')")
    public CommonResult<Long> createExpandTargetChannel(@Valid @RequestBody ExpandTargetChannelSaveReqVO createReqVO) {
        return success(expandTargetChannelService.createExpandTargetChannel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拓展目标_渠道")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:update')")
    public CommonResult<Boolean> updateExpandTargetChannel(@Valid @RequestBody ExpandTargetChannelSaveReqVO updateReqVO) {
        expandTargetChannelService.updateExpandTargetChannel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拓展目标_渠道")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:delete')")
    public CommonResult<Boolean> deleteExpandTargetChannel(@RequestParam("id") Long id) {
        expandTargetChannelService.deleteExpandTargetChannel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除拓展目标_渠道")
                @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:delete')")
    public CommonResult<Boolean> deleteExpandTargetChannelList(@RequestParam("ids") List<Long> ids) {
        expandTargetChannelService.deleteExpandTargetChannelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拓展目标_渠道")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:query')")
    public CommonResult<ExpandTargetChannelRespVO> getExpandTargetChannel(@RequestParam("id") Long id) {
        ExpandTargetChannelDO expandTargetChannel = expandTargetChannelService.getExpandTargetChannel(id);
        return success(BeanUtils.toBean(expandTargetChannel, ExpandTargetChannelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拓展目标_渠道分页")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:query')")
    public CommonResult<PageResult<ExpandTargetChannelRespVO>> getExpandTargetChannelPage(@Valid ExpandTargetChannelPageReqVO pageReqVO) {
        PageResult<ExpandTargetChannelDO> pageResult = expandTargetChannelService.getExpandTargetChannelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpandTargetChannelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拓展目标_渠道 Excel")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpandTargetChannelExcel(@Valid ExpandTargetChannelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpandTargetChannelDO> list = expandTargetChannelService.getExpandTargetChannelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拓展目标_渠道.xls", "数据", ExpandTargetChannelRespVO.class,
                        BeanUtils.toBean(list, ExpandTargetChannelRespVO.class));
    }

    @GetMapping("/checkExistByName")
    @Operation(summary = "校验渠道是否存在")
    @Parameter(name = "name", description = "渠道名称", required = true, example = "渠道名称")
    @PreAuthorize("@ss.hasPermission('san:biz-expand-target-channel:query')")
    public CommonResult<Integer> checkExistByName(@RequestParam("name") String name) {
        Integer existFlag = expandTargetChannelService.checkExistByName(name.trim());
        return success(existFlag);
    }

}